package com.adhere.common.exception.handler;

import com.adhere.common.exception.ServiceException;
import com.adhere.common.utils.RespResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * RestFul 统一异常处理
 */
@Slf4j
@RestControllerAdvice
public class GlobalRestExceptionHandler extends ResponseEntityExceptionHandler {

    // 空指针异常
    @ExceptionHandler(NullPointerException.class)
    public ResponseEntity<?> NullPointerExceptionHandler(HttpServletRequest request, HttpServletResponse response, NullPointerException e) {
        log.error("请求的路径{}", request.getRequestURI(), e);
        return new ResponseEntity<Object>(RespResultUtils.getFail(("服务端出错---空指针异常,请求路径" + request.getRequestURI())), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    // form数据校验异常,重写方法
    public ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        StringBuffer error= new StringBuffer("");
        ServletWebRequest servletWebRequest = (ServletWebRequest)request;
        HttpServletRequest servletRequest = servletWebRequest.getRequest();
         ex.getBindingResult().getAllErrors().stream().forEach(objectError ->{
             FieldError fieldError = (FieldError) objectError;
             error.append(fieldError.getField()+ ":"+fieldError.getDefaultMessage()+"，");
         });
        log.error("请求的路径{}", servletRequest.getRequestURI(),ex);
        return new ResponseEntity<Object>(RespResultUtils.getFail(("请求路径:"+servletRequest.getRequestURI()+"---数据校验异常【" + error+"】")), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    // json数据校验异常,重写方法
    public ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
        StringBuffer error= new StringBuffer("");
        ServletWebRequest servletWebRequest = (ServletWebRequest)request;
        HttpServletRequest servletRequest = servletWebRequest.getRequest();
        ex.getBindingResult().getAllErrors().stream().forEach(objectError ->{
            FieldError fieldError = (FieldError) objectError;
            error.append(fieldError.getField()+ ":"+fieldError.getDefaultMessage()+"，");
        });
        log.error("请求的路径{}", servletRequest.getRequestURI(),ex);
        return new ResponseEntity<Object>(RespResultUtils.getFail(("请求路径:"+servletRequest.getRequestURI()+"---数据校验异常【" + error+"】")), HttpStatus.INTERNAL_SERVER_ERROR);
    }


    //自定义异常处理
    @ExceptionHandler(ServiceException.class)
    public ResponseEntity<?> ServiceExceptionHandler(HttpServletRequest request, HttpServletResponse response, ServiceException e) {
        log.error("请求的路径{}", request.getRequestURI(), e);
        return new ResponseEntity<Object>(RespResultUtils.getFail(((ServiceException) e).getCode(), e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    //捕获不是自定义的异常
    @ExceptionHandler(Exception.class)
    public ResponseEntity<?> ExceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
        log.error("请求的路径{}", request.getRequestURI(), e);
        return new ResponseEntity<Object>(RespResultUtils.getFail(e.getMessage()), HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
